package com.ruoyi.netty.client;

import com.ruoyi.netty.common.Config;
import com.ruoyi.netty.rabbitmq.VehicMapper;
import com.ruoyi.netty.netty.log.NettyClientLogQueue;
import com.ruoyi.netty.utils.RabbitmqUtils;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;

public class NettyClientHandler extends ChannelInboundHandlerAdapter {


	private static final Logger log = LoggerFactory.getLogger(NettyClientHandler.class);

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		log.info("客户端与服务器建立连接成功");
		Config.ctx = ctx;
		NettyClientLogQueue.add("<p>成功与服务器建立连接</p>");
	}
	@Autowired
	private RedisTemplate<String, String> redis;

	public static RedisTemplate<String, String> redisTemplate;

	@PostConstruct
	public void getRedisTemplate() {
		redisTemplate = this.redis;
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//		log.info("接收到服务端发来的消息:" + msg);
		String msg1 = (String) msg;

//		System.out.println(substring1);

//		System.out.println(JSON.parseObject(substring1, VehicleData.class));
		// 车辆上线
		if (msg1.startsWith("vehicle_start:")){
			log.info("车辆上线"+msg1);
			VehicMapper vehicMapper = new VehicMapper();
			vehicMapper.VehicleStart(msg1);
		}
		// 车辆下线
		if (msg1.startsWith("vehicle_stop:")){
			log.info("车辆下线"+msg1);
			VehicMapper vehicMapper = new VehicMapper();
			vehicMapper.VehicleStop(msg1);
		}

		// 上传信息数据
		if (msg1.startsWith("vehicle_msg:")){
			RabbitTemplate rabbitTemplate = RabbitmqUtils.rabbit;
			Message message = MessageBuilder.withBody(msg1.getBytes(StandardCharsets.UTF_8)).build();
			rabbitTemplate.convertAndSend("businessExchange","k1",message);
			rabbitTemplate.convertAndSend("businessExchange","k2",message);
			rabbitTemplate.convertAndSend("businessExchange","k3",message);
//			log.info("rabbitmq发送成功 : " + vehic);
//			VehicleData vehicleData = JSON.parseObject(substring1, VehicleData.class);
//			vehicRabbitMq.VehicleMessage(vehicleData);
		}
		// 关闭连接
		if (msg1.startsWith("close")){
			log.info("关闭连接");
			VehicMapper vehicMapper = new VehicMapper();
			vehicMapper.VehicleClose(msg1);
		}


		NettyClientLogQueue.add("<p>接收到服务端发来的消息"+msg+"</p>");
		if ("断开".equals(msg)){
			ctx.writeAndFlush("客户端退出").addListener(ChannelFutureListener.CLOSE);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		log.info("客户端捕获异常：{}",cause.getMessage());
		cause.printStackTrace();
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		log.info("隧道切换不活跃状态");
	}


}
